<?php
/**
 * @author spenceryg
 * @date 2022/2/7
 * @time 7:46 上午
 * @focus love & life
 */

namespace app\admin\controller\sys;

use app\admin\controller\sys\business\AdminRoleBusiness;
use app\admin\controller\sys\model\AdminRoleAccessModel;
use app\admin\controller\sys\model\AdminRoleModel;
use app\admin\controller\sys\model\AdminRoleNodeModel;
use app\admin\controller\sys\model\AdminRoleUserModel;
use app\admin\controller\sys\validate\AdminRoleValidate;
use app\BaseController;
use think\App;
use think\facade\Db;
use think\facade\View;
use think\response\Json;

/**
 * 后台角色
 * Class AdminRole
 * @package app\admin\controller\sys
 */
class AdminRole extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }


    /**
     * 列表
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(): string
    {
        View::assign('data', AdminRoleModel::select());
        return View::fetch(app()->getAppPath() . 'controller/sys/view/admin_role/index.html');
    }


    /**
     * 添加
     * @return string|Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            AdminRoleBusiness::add($data);
            return success('添加角色成功');
        } else {
            $nodes = Db::name('admin_node')->where('delete_time', '=', null)->select();
            View::assign('nodes', $nodes);
            $access = Db::name('admin_access')->where('delete_time', '=', null)->select();
            View::assign('accessArr', $access);
            return View::fetch(app()->getAppPath() . 'controller/sys/view/admin_role/add.html');
        }
    }

    /**
     * 编辑
     * @return string|Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input('post.');
            try {
                AdminRoleBusiness::update($data);
                return success('修改角色成功');
            } catch (\Exception $e) {
                return error($e->getMessage());
            }
        } else {
            $id = input('id', 0);
            $r = AdminRoleModel::find($id);
            $admin_node_arr = array_map(function ($item) {
                return $item['pivot']['admin_node_id'];
            }, $r->adminNode->toArray());
            $admin_access_arr = array_map(function ($item) {
                return $item['pivot']['admin_access_id'];
            }, $r->adminAccess->toArray());
            View::assign('r', $r->getData());
            $nodes = Db::name('admin_node')->where('delete_time', '=', null)->select()->toArray();
            foreach ($nodes as $key => $node) {
                if (in_array($node['id'], $admin_node_arr)) {
                    $nodes[$key]['checked'] = true;
                }
            }
            View::assign('nodes', $nodes);
            $access = Db::name('admin_access')->where('delete_time', '=', null)->select()->toArray();
            foreach ($access as $key => $node) {
                if (in_array($node['id'], $admin_access_arr)) {
                    $access[$key]['checked'] = true;
                }
            }
            View::assign('accessArr', $access);
            return View::fetch(app()->getAppPath() . 'controller/sys/view/admin_role/update.html');
        }
    }


    /**
     * 删除
     * @return Json
     */
    public function delete(): Json
    {
        $id = input('id', 0);
        AdminRoleModel::where('id', $id)->delete();
        AdminRoleUserModel::where('admin_role_id', $id)->delete();
        AdminRoleAccessModel::where('admin_role_id', $id)->delete();
        AdminRoleNodeModel::where('admin_role_id', $id)->delete();
        return success('角色删除成功！');
    }
}
